package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.qfedu.common.JsonResult;
import com.qfedu.pojo.Dept;
import com.qfedu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guo
 */
@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/selectAll.do")
    @ResponseBody
    public Map<String, Object> selectDept(Integer page, Integer limit, String name) {
        if (null == name) {
            name = "";
        }
        Map<String, Object> map = new HashMap<>(5);
        List<Dept> list = deptService.findAllDept(page, limit, name);

        long total = ((Page) list).getTotal();

        map.put("code", 0);
        map.put("count", total);
        map.put("msg", "");
        map.put("data", list);
        return map;
    }

    @RequestMapping("/findOne.do")
    @ResponseBody
    public JsonResult findDeptById(Integer id) {
        Dept dept = deptService.findById(id);
        return new JsonResult(1, dept);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult deleteDept(Integer id) {
        deptService.deleteDeptById(id);
        return new JsonResult(1, "删除成功！");
    }

    @RequestMapping("/addDept.do")
    @ResponseBody
    public JsonResult addDept(Dept dept) {
        deptService.addDept(dept);
        return new JsonResult(1, "添加成功");
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public JsonResult updateDeptById(Dept dept) {
        System.out.println(dept);
        deptService.updateDeptById(dept);
        return new JsonResult(1, "添加成功");
    }
}
